<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryRef"
            :inline="true"
            v-show="showSearch"
        >
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-form-item label="员工姓名" prop="employeeName">
                        <el-input
                            v-model="queryParams.employeeName"
                            placeholder="请输入员工姓名"
                            clearable
                            @keyup.enter="handleQuery"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="缴纳状态" prop="isPaying">
                        <el-select v-model="isPayingModel" placeholder="请选择缴纳状态" style="width: 100%">
                            <el-option
                                v-for="item in isPayingOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click="handleQuery">搜索</el-button>
                    <el-button @click="resetQuery">重置</el-button>
                </el-col>
            </el-row>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="Plus"
                    @click="handleAdd"
                    v-hasPermi="['system:employee_social_security:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="Edit"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['system:employee_social_security:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['system:employee_social_security:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="Download"
                    @click="handleExport"
                    v-hasPermi="['system:employee_social_security:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar :showSearch="showSearch" @update:showSearch="showSearch = $event" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
            v-loading="loading"
            :data="socialSecurityList"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="员工姓名" align="center" prop="employeeName" />
            <el-table-column label="是否缴纳社保" align="center" prop="isPaying">
                <template #default="scope">
                    <dict-tag :options="sys_yes_no" :value="scope.row.isPaying"/>
                </template>
            </el-table-column>
            <el-table-column label="社保金额" align="center" prop="socialSecurityAmount" />
            <el-table-column label="开始缴纳时间" align="center" prop="startDate" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="结束缴纳时间" align="center" prop="endDate" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.updateTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button
                        link
                        type="primary"
                        icon="Edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:employee_social_security:edit']"
                    >修改</el-button>
                    <el-button
                        link
                        type="primary"
                        icon="Delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['system:employee_social_security:remove']"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            :current-page="queryParams.pageNum"
            :page-size="queryParams.pageSize"
            @update:current-page="queryParams.pageNum = $event"
            @update:page-size="queryParams.pageSize = $event"
            @pagination="getList"
        />

        <!-- 添加或修改员工社保对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="socialSecurityRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="员工姓名" prop="employeeName">
                    <el-input v-model="form.employeeName" placeholder="请输入员工姓名" />
                </el-form-item>
                <el-form-item label="是否缴纳社保" prop="isPaying">
                    <el-radio-group v-model="form.isPaying">
                        <el-radio
                            v-for="dict in sys_yes_no"
                            :key="dict.value"
                            :label="dict.value"
                        >{{ dict.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="社保金额" prop="socialSecurityAmount">
                    <el-input-number v-model="form.socialSecurityAmount" :precision="2" :step="0.01" :min="0" disabled />
                </el-form-item>
                <el-form-item label="开始缴纳时间" prop="startDate">
                    <el-date-picker
                        v-model="form.startDate"
                        type="date"
                        placeholder="选择开始缴纳时间"
                        value-format="YYYY-MM-DD"
                    />
                </el-form-item>
                <el-form-item label="结束缴纳时间" prop="endDate">
                    <el-date-picker
                        v-model="form.endDate"
                        type="date"
                        placeholder="选择结束缴纳时间"
                        value-format="YYYY-MM-DD"
                    />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="EmployeeSocialSecurity">
import {
    listEmployeeSocialSecurity,
    getEmployeeSocialSecurity,
    delEmployeeSocialSecurity,
    addEmployeeSocialSecurity,
    updateEmployeeSocialSecurity,
    exportEmployeeSocialSecurity
} from '@/api/system/employee_social_security'

const { proxy } = getCurrentInstance()
const { sys_yes_no } = proxy.useDict('sys_yes_no')

const socialSecurityList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

const data = reactive({
    form: {
        id: undefined,
        employeeId: undefined,
        employeeName: undefined,
        isPaying: '0',
        socialSecurityAmount: 0,
        startDate: undefined,
        endDate: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined
    },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeName: undefined,
        isPaying: undefined
    },
    rules: {
        employeeName: [
            { required: true, message: '员工姓名不能为空', trigger: 'blur' }
        ],
        isPaying: [
            { required: true, message: '是否缴纳社保不能为空', trigger: 'change' }
        ],
        startDate: [
            { required: true, message: '开始缴纳时间不能为空', trigger: 'blur' }
        ],
        endDate: [
            { required: true, message: '结束缴纳时间不能为空', trigger: 'blur' }
        ]
    }
})

const { queryParams, form, rules } = toRefs(data)

const isPayingOptions = [
    { label: '缴纳', value: '1' },
    { label: '不缴纳', value: '0' }
]

const isPayingModel = computed({
    get: () => String(queryParams.value.isPaying ?? ''),
    set: val => { queryParams.value.isPaying = String(val) }
})

/** 查询员工社保列表 */
function getList() {
    loading.value = true
    listEmployeeSocialSecurity(queryParams.value).then(response => {
        socialSecurityList.value = response.data.rows
        total.value = response.data.total
        loading.value = false
    })
}

/** 取消按钮 */
function cancel() {
    open.value = false
    reset()
}

/** 表单重置 */
function reset() {
    form.value = {
        id: undefined,
        employeeId: undefined,
        employeeName: undefined,
        isPaying: '0',
        socialSecurityAmount: 0,
        startDate: undefined,
        endDate: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined
    }
    proxy.resetForm('socialSecurityRef')
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef')
    handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id)
    single.value = selection.length !== 1
    multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
    reset()
    open.value = true
    title.value = '添加员工社保'
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset()
    const id = row.id || ids.value[0]
    getEmployeeSocialSecurity(id).then(response => {
        form.value = response.data
        open.value = true
        title.value = '修改员工社保'
    })
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['socialSecurityRef'].validate(valid => {
        if (valid) {
            if (form.value.id != undefined) {
                updateEmployeeSocialSecurity(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功')
                    open.value = false
                    getList()
                })
            } else {
                addEmployeeSocialSecurity(form.value).then(response => {
                    proxy.$modal.msgSuccess('新增成功')
                    open.value = false
                    getList()
                })
            }
        }
    })
}

/** 删除按钮操作 */
function handleDelete(row) {
    const idList = row.id ? [row.id] : ids.value
    proxy.$modal.confirm('是否确认删除员工社保编号为"' + idList + '"的数据项？').then(function() {
        return delEmployeeSocialSecurity(idList)
    }).then(() => {
        getList()
        proxy.$modal.msgSuccess('删除成功')
    }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('system/employee_social_security/export', {
        ...queryParams.value
    }, `employee_social_security_${new Date().getTime()}.xlsx`)
}

// 监听是否缴纳社保的变化
watch(() => form.value.isPaying, (newVal) => {
    if (newVal === '1') {
        form.value.socialSecurityAmount = 1400.00
    } else {
        form.value.socialSecurityAmount = 0.00
    }
})

getList()
</script> 